package com.pinyuana.dragongame

import android.app.Application
import android.content.Context
import android.content.res.Configuration
import android.os.Build
import android.view.Gravity
import androidx.multidex.BuildConfig
import androidx.multidex.MultiDex
import coil.Coil
import coil.ImageLoader
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import coil.decode.SvgDecoder
import coil.memory.MemoryCache
import coil.request.CachePolicy
import com.alibaba.android.arouter.launcher.ARouter
import com.android.base_network.CacheKeys
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.cardetective.shopcar.R
import com.cardetective.shopcar.utils.LanguageUtil
import com.kongzue.dialogx.DialogX
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import top.wangchenyan.common.CommonApp
import java.util.Locale


class CtzApplication : Application() {
    companion object{
        lateinit var token: String
        lateinit var appContext: Context

        init {
            SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, _ ->
                ClassicsHeader(context)
            }
        }
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    override fun onCreate() {
        super.onCreate()
        appContext = this
        token = SPUtils.getInstance().getString(CacheKeys.token)
        LogUtils.e("token:$token")
        DialogX.init(this);
        initLogUtils()
        initARouter()
        initCoil()
//        initLanguageWork()
        initCommonAPP()
        initToast()

    }

    //Toast设置统一样式
    fun initToast() {
        //Kotlin中
        val defaultMaker = ToastUtils.getDefaultMaker()
        defaultMaker.setBgColor(this.resources.getColor(R.color.black))
        defaultMaker.setGravity(Gravity.CENTER,0,0)
        defaultMaker.setTextColor(this.resources.getColor(R.color.white))
    }

    private fun initCommonAPP() {
        CommonApp.init {
            test = BuildConfig.DEBUG
            imageLoaderConfig {  }
        }
    }

    private fun initLogUtils() {
        LogUtils.getConfig().run {
            isLogSwitch = BuildConfig.DEBUG
            isSingleTagSwitch = true
        }
    }

    private fun initARouter() {
        if (BuildConfig.DEBUG) {
            ARouter.openLog()
            ARouter.openDebug()
        }
        ARouter.init(this)
    }

    private fun initCoil() {
        val imageLoader = ImageLoader.Builder(this)
            .memoryCache {
                MemoryCache.Builder(this)
                    .maxSizePercent(0.25)
                    .build()
            }
            .memoryCachePolicy(CachePolicy.ENABLED)
            .diskCachePolicy(CachePolicy.ENABLED)
            .networkCachePolicy(CachePolicy.ENABLED)
            .components {
                //支持gif
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
                    add(ImageDecoderDecoder.Factory())
                } else {
                    add(GifDecoder.Factory())
                }
                //支持svg
                add(SvgDecoder.Factory())
            }
            .build()

        Coil.setImageLoader(imageLoader)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
//        initLanguageWork()
    }

    private fun initLanguageWork(){
        val locale: Locale = LanguageUtil().getLocale(this)
        LanguageUtil().updateLocale(this, locale)
    }

}